package onlinecalsys.client.view;

import java.rmi.RemoteException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import onlinecalsys.client.controller.CalsysClientController;
import onlinecalsys.client.controller.ClientController;
import onlinecalsys.client.exceptions.DateFormatException;
import onlinecalsys.client.view.gui.Error;
import onlinecalsys.client.view.gui.MainGUI;
import onlinecalsys.client.view.gui.SharedAppointment;
import onlinecalsys.server.objects.Appointment;
import onlinecalsys.server.objects.CalsysAppointment;
import onlinecalsys.server.objects.CalsysCalendar;

public class MainView implements View {

	private static MainView view;
	private ClientController controller;
	private List<String> actualAppointments;
	private String today;
	private String previousDay;
	private String nextDay;
	private MainGUI gui;
	private String username;
	
	

	private MainView(String username) throws ParseException, RemoteException {
		gui = new MainGUI(this);
		today = CalsysCalendar.today();
		previousDay = CalsysCalendar.getDayBefore(today);
		nextDay = CalsysCalendar.getNextDay(today);
		this.username = username;
		controller = new CalsysClientController(this);
		controller.onlineNow();

	}

	public static MainView getInstance(String username) throws ParseException,
			RemoteException {
		if (view == null) {
			view = new MainView(username);
		}

		return view;
	}

	@Override
	public boolean newAppointment(String appointment, String date, String hour) {
		try {
			System.out.println("view: " + date);
			String[] dateSplitted = date.split("/", 3);

			CalsysCalendar cal = new CalsysCalendar(Integer
					.valueOf(dateSplitted[2]),
					Integer.valueOf(dateSplitted[1]), Integer
							.valueOf(dateSplitted[0]), Integer.valueOf(hour));

			// checking to see if the calendar object is correct and in range.
			if (!cal.isValidDate()) {
				throw new DateFormatException();
			}

			Appointment app = new CalsysAppointment(username, appointment, cal);
			controller.addAppointment(app);
			return true;
		} catch (RemoteException e) {
			errorGui("Sorry the is either no connection to the server or there is an internal server problem");
			return false;
		} catch (DateFormatException e) {
			errorGui("Sorry wrong input");
			return false;
		} catch (Exception e) {
			errorGui("Sorry the is either no connection to the server or there is an internal server problem");
			return false;
		}

	}

	@Override
	public void updateAppoinments(String date) {
		try {
			ArrayList<ArrayList<String>> applist = new ArrayList<ArrayList<String>>();
			applist = controller.getAppointments(date, username);

			// create appointments in one string file
			String appointments = appointmentsAsString(date, applist);

			gui.setTextAppointment(appointments);

		} catch (RemoteException e) {
			errorGui("Sorry the is either no connection to the server or there is an internal server problem");
		}
	}

	private String appointmentsAsString(String date,
			ArrayList<ArrayList<String>> applist) {
		String ls = System.getProperty("line.separator");
		String appointments = "Date: " + date;
		appointments += ls;
		for (ArrayList<String> s : applist) {
			appointments += "*************************";
			appointments += ls;
			appointments += "ID: " + s.get(0);
			appointments += ls;
			appointments += "Time: " + s.get(1) + " o'clock";
			appointments += ls;
			appointments += "Appointment: " + s.get(3);
			appointments += ls;
			if (s.size() == 5) {
				appointments += "Shared from: " + s.get(4);
				appointments += ls;
			}
		}
		appointments += "*************************";
		return appointments;
	}

	@Override
	public boolean deleteAppointment(int appointmentId) {
		try {
			if (!controller.deleteAppointment(appointmentId, username)) {
				errorGui("Sorry, the number is not a valid ID");
				return false;
			} else {
				return true;
			}
		} catch (RemoteException e) {
			errorGui("Sorry the is either no connection to the server or there is an internal server problem");
			return false;
		}

	}

	public boolean saveNote(String note) {
		try {
			return controller.saveNote(note, username);
		} catch (RemoteException e) {
			return false;
		}

	}
	public void getNote()  {
		
		try {
			gui.setNote(controller.getNote(username));
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
			
		
			}
	

	public boolean shareAppointment(int id, String shareWithUser) {
		try {
			boolean status = controller.shareAppointment(username, id,
					shareWithUser);
			if (!status) {
				errorGui("Sorry you input is wrong, either the id of the appointment or username OR the appointment is already shard");
				return false;
			}
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
			return false;
		}

	}

	public void errorGui(String errorMessage) {
		new Error(errorMessage);
	}

	@Override
	public String getUsername() {
		return username;
	}
	
	@Override
	public void setNewSharedAppointments() {
		gui.setNewSharedAppointmentes();
	}



	@Override
	public void getSharedAppointments() {
		try {
			ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
			list = controller.getSharedAppointments(this.username);
			String app = appointmentsAsString("SharedAppointments", list);
			gui.newSharedAppointments(app);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	

	




}
